package com.zzqstart.cateorder.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzqstart.cateorder.entity.CreateCondition;
import com.zzqstart.cateorder.entity.Order;
import com.zzqstart.cateorder.entity.OrderSearh;
import com.zzqstart.cateorder.mapper.OrderMapper;
import com.zzqstart.cateorder.remote.CateServiceRemote;
import com.zzqstart.cateorder.remote.UserCentRemote;
import com.zzqstart.cateorder.service.OrderService;
import com.zzqstart.commonutils.R;
import com.zzqstart.commonutils.entity.RecipeDetailFrontVo;
import com.zzqstart.commonutils.entity.UcenterMember;
import com.zzqstart.servicebase.exceptionhandler.CateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-03
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CateServiceRemote cateServiceRemote;

    @Autowired
    private UserCentRemote userCentRemote;

    @Override
    public String createOrder(CreateCondition createCondition, String memberId) {

        BigDecimal totalFee = createCondition.getTotalFee();
        if (totalFee.compareTo(new BigDecimal(0)) != 1) {
            throw new CateException(20001, "数字不能比零小");
        }

        String recipeId = createCondition.getRecipeId();
        RecipeDetailFrontVo recipe = cateServiceRemote.getRecipeById(recipeId);

        UcenterMember member = userCentRemote.getMemberInfoByMemberId(memberId);

        //创建订单
        Order order = new Order();
        String OrderNo = new SimpleDateFormat("yyyyMMddss").format(new Date()) + UUID.randomUUID().toString().substring(0, 4);
        order.setOrderNo(OrderNo);
        order.setRecipeId(recipeId);
        order.setRecipeName(recipe.getTitle());
        order.setRecipeCover(recipe.getMainImgUrl());
        order.setPublishNickname(recipe.getNickname());
        order.setMemberId(memberId);
        order.setNickname(member.getNickname());
        order.setMobile(member.getMobile());
        order.setStatus(0);
        order.setPayType(1);
        order.setTotalFee(totalFee);
        baseMapper.insert(order);
        return order.getOrderNo();
    }

    @Override
    public Map<String, Object> getBackOrderInfo(OrderSearh searchObj, Long current, Long limit) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(searchObj)) {
            if (!StringUtils.isEmpty(searchObj.getNickname())) {
                wrapper.like("nickname", searchObj.getNickname());
            }

            if (!StringUtils.isEmpty(searchObj.getRecipeName())) {
                wrapper.like("recipe_name", searchObj.getRecipeName());
            }

            if (!StringUtils.isEmpty(searchObj.getRecipeId())) {
                wrapper.eq("recipe_id", searchObj.getRecipeId());
            }

            if (!StringUtils.isEmpty(searchObj.getTime())) {
                wrapper.le("create_time", searchObj.getTime());
            }

            if (!StringUtils.isEmpty(searchObj.getStatus())) {
                wrapper.eq("status", searchObj.getStatus());
            }
            if (!StringUtils.isEmpty(searchObj.getOrderNo())) {
                wrapper.eq("order_no", searchObj.getOrderNo());
            }
            if (!StringUtils.isEmpty(searchObj.getPublishNickname())) {
                wrapper.like("publish_nickname", searchObj.getPublishNickname());
            }
        }
        Page<Order> page = new Page<>(current, limit);

        wrapper.orderByDesc("create_time");
        baseMapper.selectPage(page, wrapper);

        List<Order> list = page.getRecords();
        long total = page.getTotal();
        long currentPage = page.getCurrent();

        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        map.put("currentPage", currentPage);

        return map;
    }

    @Override
    public Boolean deleteOrder(String memberId, String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orderId);
        Order order = baseMapper.selectOne(wrapper);

        if (!memberId.equals("admin")) {
            if (!memberId.equals(order.getMemberId())) {
                throw new CateException(20001, "权限不足");
            }
        }

        int delete = baseMapper.delete(wrapper);

        return delete > 0;
    }
}
